package com.tbit.uqbike.webmanager.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.tbit.uqbike.constant.OperaPswConstant;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.uqbike.enums.SystemCodeEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.*;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.value.Page;
import com.tbit.uqbike.object.value.QueryVO;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.common.entity.view.Result;
import com.tbit.uqbike.service.business.*;
import com.tbit.uqbike.webmanager.util.PageHelperUtil;
import com.tbit.uqbike.webmanager.util.ServletUtils;
import com.tbit.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;

import static com.tbit.uqbike.webmanager.controller.BaseController.getLogin;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 11:45 2020/4/8
 */
@Validated
@RestController
@RequestMapping("/payConfig")
public class PayConfigController {

    @Autowired
    private ZfbPayConfigService zfbPayConfigService;
    @Autowired
    private MpBrandConfigService mpBrandConfigService;
    @Autowired
    private SmsAuthConfigService smsAuthConfigService;
    @Autowired
    private WeixinPayConfigService weixinPayConfigService;
    @Autowired
    private PayPalConfigService payPalConfigService;
    @Autowired
    private CallConfigService callConfigService;
    @Autowired
    private WeChatOperationConfigurationService weChatOperationConfigurationService;
    @Autowired
    private AccountPhoneLoginService accountPhoneLoginService;
    @Autowired
    private AccountSmsModelService accountSmsModelService;
    @Autowired
    private AdaPayConfigService adaPayConfigService;

    /**
     * 获取支付宝配置信息
     *
     * @param accountId
     * @return
     */
    @RequestMapping("/getZFBConfig")
    public JsonResult getZFBConfig(@NotNull(message = "ver.accountId.notNull") Integer accountId, Integer type) {
        check(ServletUtils.getRequest(), null);
        ZfbPayConfig zfbPayConfig = zfbPayConfigService.getByAccountId(accountId, type);
        return JsonResult.ok(zfbPayConfig);
    }

    /**
     * 获取微信配置信息
     *
     * @param accountId
     * @param type
     * @return
     */
    @RequestMapping("/getWXConfig")
    public JsonResult getWXConfig(@NotNull(message = "ver.accountId.notNull") Integer accountId, Integer type) {
        check(ServletUtils.getRequest(), null);
        WeixinPayConfig weixinPayConfig = weixinPayConfigService.getByAccountId(accountId, type);
        return JsonResult.ok(weixinPayConfig);
    }

    /**
     * 获取短信配置信息
     *
     * @param accountId
     * @return
     */
    @RequestMapping("/getSmsConfig")
    public JsonResult getSmsConfig(@NotNull(message = "ver.accountId.notNull") Integer accountId) {
        check(ServletUtils.getRequest(), null);
        SmsAuthConfig smsAuthConfig = smsAuthConfigService.getByAccountId(accountId);
        return JsonResult.ok(smsAuthConfig);
    }

    /**
     * 获取小程序配置
     *
     * @param accountId
     * @return
     */
    @RequestMapping("/getMPConfig")
    public JsonResult getMPConfig(@NotNull(message = "ver.accountId.notNull") Integer accountId) {
        check(ServletUtils.getRequest(), null);
        MpBrandConfig mpBrandConfig = mpBrandConfigService.getByAccountId(accountId);
        return JsonResult.ok(mpBrandConfig);
    }

    /**
     * 获取国际paypal配置
     *
     * @param accountId
     * @return
     */
    @RequestMapping("/getPaypalConfig")
    public JsonResult getPaypalConfig(@NotNull(message = "ver.accountId.notNull") Integer accountId) {
        check(ServletUtils.getRequest(), null);
        PayPalConfig payPalConfig = payPalConfigService.getByAccountId(accountId);
        return JsonResult.ok(payPalConfig);
    }

    /**
     * 修改支付宝配置
     *
     * @param zfbPayConfig
     * @param
     * @return
     */
    @RequestMapping("/editZFBConfig")
    public JsonResult editZFBConfig(ZfbPayConfig zfbPayConfig, @NotNull(message = "ver.secret.notNull") String operaPsw) {
        check(ServletUtils.getRequest(), operaPsw);
        zfbPayConfig.setOpId(BaseController.getLogin(ServletUtils.getRequest()).getAccountUserId());
        zfbPayConfigService.add(zfbPayConfig);
        return JsonResult.ok();
    }

    /**
     * 修改微信配置
     *
     * @param weixinPayConfig
     * @param
     * @param type
     * @return
     */
    @RequestMapping("/editWXConfig")
    public JsonResult editWXConfig(WeixinPayConfig weixinPayConfig, @NotNull(message = "ver.secret.notNull") String operaPsw, Integer type, HttpServletRequest request) {
        check(ServletUtils.getRequest(), operaPsw);
        weixinPayConfig.setOpId(BaseController.getLogin(ServletUtils.getRequest()).getAccountUserId());
        weixinPayConfigService.edit(weixinPayConfig);
        return JsonResult.ok();
    }

    /**
     * 删除微信配置
     *
     * @param accountId 区域id/品牌id
     * @param type      微信商户平台类型,1：APP支付商户平台；2：微信小程序商户平台
     * @param operaPsw  操作密钥
     * @return
     */
    @DeleteMapping("/deleteWxConfig")
    public Result deleteWxConfig(Integer accountId, Integer type, @NotNull(message = "ver.secret.notNull") String operaPsw) {
        Assert.notNulls("参数有误", accountId, type, operaPsw);
        check(ServletUtils.getRequest(), operaPsw);
        weixinPayConfigService.deleteWxConfig(accountId, type);
        return Result.success();
    }

    /**
     * 修改短信配置
     *
     * @param smsAuthConfig
     * @param
     * @return
     */
    @RequestMapping("/editSmsConfig")
    public JsonResult editSmsConfig(SmsAuthConfig smsAuthConfig, @NotNull(message = "ver.secret.notNull") String operaPsw) {
        check(ServletUtils.getRequest(), operaPsw);
        smsAuthConfigService.add(smsAuthConfig);
        return JsonResult.ok();
    }

    /**
     * 修改小程序配置
     *
     * @param mpBrandConfig
     * @param
     * @return
     */
    @RequestMapping("/editMPConfig")
    public JsonResult editMPConfig(MpBrandConfig mpBrandConfig, @NotNull(message = "ver.secret.notNull") String operaPsw) {
        check(ServletUtils.getRequest(), operaPsw);
        mpBrandConfigService.add(mpBrandConfig);
        return JsonResult.ok();
    }

    @RequestMapping("/editPaypalConfig")
    public JsonResult editPaypalConfig(PayPalConfig payPalConfig, @NotNull(message = "ver.secret.notNull") String operaPsw) {
        check(ServletUtils.getRequest(), operaPsw);
        payPalConfigService.add(payPalConfig);
        return JsonResult.ok();
    }

    /**
     * 获取寻呼配置
     *
     * @param accountId
     * @return
     */
    @RequestMapping("/getCallConfig")
    public JsonResult getCallConfig(@NotNull(message = "ver.accountId.notNull") Integer accountId) {
        check(ServletUtils.getRequest(), null);
        CallConfig callConfig = callConfigService.getByAccountId(accountId);
        return JsonResult.ok(callConfig);
    }

    /**
     * 新增寻呼配置
     *
     * @param callConfig
     * @param operaPsw
     * @return
     */
    @RequestMapping("/editCallConfig")
    public JsonResult editCallConfig(CallConfig callConfig,
                                     @NotNull(message = "ver.secret.notNull") String operaPsw) {
        check(ServletUtils.getRequest(), operaPsw);
        callConfigService.insert(callConfig);
        return JsonResult.ok();
    }

    /**
     * @param wechat
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description:添加或修改微信运营配置
     * @createDate 2020/9/28 19:48
     */
    @RequestMapping("/editWeChat")
    public JsonResult editWeChat(WeChatOperationConfiguration wechat,
                                 @NotNull(message = "ver.secret.notNull") String operaPsw) {
        /**验证权限*/
        check(ServletUtils.getRequest(), operaPsw);
        wechat.setOpId(BaseController.getLogin(ServletUtils.getRequest()).getAccountUserId());
        weChatOperationConfigurationService.add(wechat);
        return JsonResult.ok();
    }

    /**
     * @param
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 根据品牌id获取微信运营配置
     * @createDate 2020/9/28 19:48
     */
    @RequestMapping("/getWeChatByBrandId")
    public JsonResult getByBrandId(Integer brandId) {
        /**验证权限*/
        check(ServletUtils.getRequest(), null);
        WeChatOperationConfiguration wechat = weChatOperationConfigurationService.getByBrandId(brandId);
        return JsonResult.ok(wechat);
    }

    /**
     * @param ob
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 获取一键登录配置
     * @createDate 2020/11/11 14:53
     */
    @RequestMapping("/getAccountPhoneLogin")
    public JsonResult getAccountPhoneLogin(@RequestBody(required = false) QueryVO ob) {
        AccountPhoneLogin phoneLogin = JSON.toJavaObject(ob.getObject(), AccountPhoneLogin.class);
        check(ServletUtils.getRequest(), phoneLogin.getOperaPsw());
        phoneLogin = accountPhoneLoginService.getAccountPhoneLogin(phoneLogin);
        return JsonResult.ok(phoneLogin);
    }

    /**
     * @param ob
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 编辑一键登录配置
     * @createDate 2020/11/11 15:20
     */
    @RequestMapping("/editAccountPhoneLogin")
    public JsonResult editAccountPhoneLogin(@RequestBody(required = false) QueryVO ob) {
        AccountPhoneLogin phoneLogin = JSON.toJavaObject(ob.getObject(), AccountPhoneLogin.class);
        check(ServletUtils.getRequest(), phoneLogin.getOperaPsw());
        accountPhoneLoginService.editAccountPhoneLogin(phoneLogin);
        return JsonResult.ok();
    }

    /**
     * @return com.tbit.uqbike.object.view.JsonResult
     * @Description: 配置其他短信模板
     * @createDate 2020/11/24 13:59
     */
    @PostMapping("/getAccountSmsModel")
    public JsonResult getAccountSmsModel(AccountSmsModel accountSmsModel) {
        check(ServletUtils.getRequest(), accountSmsModel.getOperaPsw());
        accountSmsModel.setPage(new Page(accountSmsModel.getPageNO(), accountSmsModel.getRowCount()));
        PageHelperUtil.initPage(accountSmsModel.getPage());
        PageInfo pageInfo = new PageInfo(accountSmsModelService.queryAllByLimit(accountSmsModel));
        return JsonResult.ok(pageInfo.getList(), (int) pageInfo.getTotal());
    }

    @PostMapping("/insertAccountSmsModel")
    public JsonResult insertAccountSmsModel(AccountSmsModel accountSmsModel) {
        check(ServletUtils.getRequest(), accountSmsModel.getOperaPsw());
        accountSmsModelService.insert(accountSmsModel);
        return JsonResult.ok();
    }

    @PostMapping("/editAccountSmsModel")
    public JsonResult editAccountSmsModel(AccountSmsModel accountSmsModel) {
        check(ServletUtils.getRequest(), accountSmsModel.getOperaPsw());
        accountSmsModelService.update(accountSmsModel);
        return JsonResult.ok();
    }

    @PostMapping("/deleteAccountSmsModel")
    public JsonResult deleteAccountSmsModel(AccountSmsModel accountSmsModel) {
        check(ServletUtils.getRequest(), accountSmsModel.getOperaPsw());
        accountSmsModelService.deleteById(accountSmsModel);
        return JsonResult.ok();
    }

    @PostMapping("/editAdaPayConfig")
    public JsonResult editAdaPayConfig(@RequestBody AdaPayConfig adaPayConfig) {
        check(ServletUtils.getRequest(), adaPayConfig.getOperaPsw());
        adaPayConfig.setOpId(BaseController.getLogin(ServletUtils.getRequest()).getAccountUserId());
        adaPayConfigService.addAdaPayConfig(adaPayConfig);
        return JsonResult.ok();
    }

    @PostMapping("/getAdaPayConfig")
    public JsonResult getAdaPayConfig(AdaPayConfig adaPayConfig) {
        AdaPayConfig config = adaPayConfigService.getInfoByAccountIdNew(adaPayConfig);
        return JsonResult.ok(config);
    }

    /**
     * 权限校验
     */
    private void check(HttpServletRequest request, String opeatePsw) {
        LoginInfo loginInfo = getLogin(request);
        if (!loginInfo.type.equals(UserConstant.USER_ADMIN)) {
            throw new BaseException(SystemCodeEnum.NO_PRIVILEGE);
        }
        /**操作密码校验*/
        if (opeatePsw != null && !opeatePsw.equals(OperaPswConstant.ADMIN_OPERA_PSW)) {
            throw new BaseException(SystemCodeEnum.OPERA_PAW_ERROR);
        }

    }

}
